package com.leetcode;

import java.util.Locale;
import java.util.Scanner;

public class 字符串分割 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数据：");
        while (scanner.hasNextLine()) {
            //1.第一行数据为int类型
            int k = Integer.parseInt(scanner.nextLine());
            //2.第二行数据为String类型
            String str = scanner.nextLine();
            String[] splits = str.split("-");
            StringBuilder start = new StringBuilder(splits[0]);
            StringBuilder s = new StringBuilder();
            for (int i = 1; i < splits.length; i++) {
                s.append(splits[i]);
            }
            int index = 0;
            while (s.length() - index >= k) {
                String singleResult = changeLowOrUpp(s.substring(index , index + k));
                start.append("-").append(singleResult);
                index += k;
            }
            if (s.length() - index > 0) {
                start.append("-").append(changeLowOrUpp(s.substring(index)));
            }
            System.out.println(start);
        }
    }

    //判断大小写，并转换
    private static String changeLowOrUpp(String substring) {
        //大写的个数
        int UpperLength = substring.length() - substring.replaceAll("[A-Z]", "").length();
        //小写的个数
        int LowerLength = substring.length() - substring.replaceAll("[a-z]", "").length();
        if (UpperLength > LowerLength) {
            return substring.toUpperCase(Locale.ROOT);
        } else if (UpperLength < LowerLength) {
            return substring.toLowerCase(Locale.ROOT);
        }
        return substring;
    }
}
